<template>
  <div class="page-header-index-wide">
    <a-row :gutter="24">
      <a-col :md="12" :sm="24" :style="{ marginBottom: '24px' }" :xl="6">
        <chart-card :loading="loading" title="设备统计" :total="(deviceState.offline+deviceState.online) | NumberFormat">
          <a-tooltip slot="action" title="刷新" @click="getDeviceState">
            <a-icon type="info-circle-o"/>
          </a-tooltip>
          <div>
            <trend flag="up" style="margin-right: 16px;">
              <span slot="term">在线</span>
              {{ deviceState.online | NumberFormat }}
            </trend>
            <trend flag="down">
              <span slot="term">离线</span>
              {{ deviceState.offline | NumberFormat }}
            </trend>
          </div>
          <template slot="footer">&nbsp;</template>
        </chart-card>
      </a-col>
      <a-col :md="12" :sm="24" :style="{ marginBottom: '24px' }" :xl="6">
        <chart-card :loading="loading" :total="dataCount.total | NumberFormat" title="数据量">
          <a-tooltip slot="action" title="刷新" @click="getDataCount">
            <a-icon type="info-circle-o"/>
          </a-tooltip>
          <mini-area :dataSource="dataCount.countSource" x="时间" y="数据量"></mini-area>
          <template slot="footer">日数据量<span> {{ dataCount.dailyCount | NumberFormat }}</span></template>
        </chart-card>
      </a-col>
      <a-col :md="12" :sm="24" :style="{ marginBottom: '24px' }" :xl="6">
        <chart-card :loading="loading" :total="6560 | NumberFormat" title="支付笔数">
          <a-tooltip slot="action" title="指标说明">
            <a-icon type="info-circle-o"/>
          </a-tooltip>
          <div>
            <mini-bar :height="40"/>
          </div>
          <template slot="footer">转化率 <span>60%</span></template>
        </chart-card>
      </a-col>
      <a-col :md="12" :sm="24" :style="{ marginBottom: '24px' }" :xl="6">
        <chart-card :loading="loading" title="运营活动效果" total="78%">
          <a-tooltip slot="action" title="指标说明">
            <a-icon type="info-circle-o"/>
          </a-tooltip>
          <div>
            <mini-progress :height="8" :percentage="78" :target="78" color="rgb(19, 194, 194)"/>
          </div>
          <template slot="footer">
            <trend flag="down" style="margin-right: 16px;">
              <span slot="term">同周比</span>
              12%
            </trend>
            <trend flag="up">
              <span slot="term">日环比</span>
              80%
            </trend>
          </template>
        </chart-card>
      </a-col>
    </a-row>

    <a-card :body-style="{padding: '0'}" :bordered="false" :loading="loading">
      <div class="salesCard">
        <a-tabs :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}" default-active-key="1" size="large">
          <a-tab-pane key="1" loading="true" tab="访问量">
            <a-card :bordered="false">
              <a-alert :showIcon="true" type="info">
                <div slot="message">
                  近一周访问量
                </div>
              </a-alert>
              <a-row style="margin-top: 10px">
                <a-col :span="6">
                  <head-info :content="loginfo.todayIp" title="今日IP"></head-info>
                </a-col>
                <a-col :span="2">
                  <a-spin class='circle-cust'>
                    <a-icon slot="indicator" style="font-size: 24px" type="environment"/>
                  </a-spin>
                </a-col>
                <a-col :span="6">
                  <head-info :content="loginfo.todayVisitCount" title="今日访问"></head-info>
                </a-col>
                <a-col :span="2">
                  <a-spin class='circle-cust'>
                    <a-icon slot="indicator" style="font-size: 24px" type="team"/>
                  </a-spin>
                </a-col>
                <a-col :span="6">
                  <head-info :content="loginfo.totalVisitCount" title="总访问量"></head-info>
                </a-col>
                <a-col :span="2">
                  <a-spin class='circle-cust'>
                    <a-icon slot="indicator" style="font-size: 24px" type="rise"/>
                  </a-spin>
                </a-col>
              </a-row>
              <line-chart-multid :dataSource="visitInfo" :fields="visitFields"></line-chart-multid>
            </a-card>
          </a-tab-pane>
          <a-tab-pane key="2" loading="true" tab="服务器信息">
            <SystemInfo></SystemInfo>
          </a-tab-pane>
          <a-tab-pane key="3" loading="true" tab="磁盘信息">
            <disk-monitoring></disk-monitoring>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>
  </div>
</template>

<script>
import ChartCard from '@/components/ChartCard'
import ACol from 'ant-design-vue/es/grid/Col'
import ATooltip from 'ant-design-vue/es/tooltip/Tooltip'
import MiniArea from '@/components/chart/MiniArea'
import MiniBar from '@/components/chart/MiniBar'
import MiniProgress from '@/components/chart/MiniProgress'
import RankList from '@/components/chart/RankList'
import Bar from '@/components/chart/Bar'
import LineChartMultid from '@/components/chart/LineChartMultid'
import HeadInfo from '@/components/tools/HeadInfo'

import SystemInfo from '@/views/modules/monitor/SystemInfo'

import Trend from '@/components/Trend'
import {getLoginfo, getVisitInfo} from '@/api/api'
import DiskMonitoring from '@views/modules/monitor/DiskMonitoring'
import {getAction} from '@api/manage'
import moment from 'dayjs'

export default {
  name: 'IndexChart',
  components: {
    DiskMonitoring,
    ATooltip,
    ACol,
    ChartCard,
    MiniArea,
    MiniBar,
    MiniProgress,
    RankList,
    Bar,
    Trend,
    LineChartMultid,
    HeadInfo,
    SystemInfo,
  },
  data() {
    return {
      loading: true,
      center: null,
      loginfo: {},
      deviceState: {},
      dataCount: {
        total: 0,
        dailyCount: 0,
        countSource: []
      },
      visitFields: ['ip', 'visit'],
      visitInfo: [],
      indicator: <a-icon type="loading" style="font-size: 24px" spin/>
    }
  },
  mounted() {
    setTimeout(() => {
      this.loading = !this.loading
    }, 400)
    this.initLogInfo()
    this.getDeviceState()
    this.getDataCount()
  },
  methods: {
    getDataCount() {
      let scope = this
      this.dataCount = {
        total: 0,
        dailyCount: 0,
        countSource: []
      }
      getAction('display/getCountDailyDataByMonth').then(res => {
        if (res.success) {
          scope.dataCount.countSource = res.result.map(data => {
            scope.dataCount.total += data.count
            if (moment(new Date()).format('YYYY-MM-DD') === data.datetime) {
              scope.dataCount.dailyCount = data.count
            }
            return {
              x: data.datetime,
              y: data.count
            }
          })
          console.log(scope.dataCount)
        }
      })
    },
    getDeviceState() {
      let scope = this
      getAction('display/getDeviceState').then(res => {
        if (res.success) {
          scope.deviceState = res.result
        }
      })
    },
    initLogInfo() {
      getLoginfo(null).then((res) => {
        if (res.success) {
          Object.keys(res.result).forEach(key => {
            res.result[key] = res.result[key] + ''
          })
          this.loginfo = res.result
        }
      })
      getVisitInfo().then(res => {
        if (res.success) {
          this.visitInfo = res.result
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.circle-cust {
  position: relative;
  top: 28px;
  left: -100%;
}

.extra-wrapper {
  line-height: 55px;
  padding-right: 24px;

  .extra-item {
    display: inline-block;
    margin-right: 24px;

    a {
      margin-left: 24px;
    }
  }
}

/* 首页访问量统计 */
.head-info {
  position: relative;
  text-align: left;
  padding: 0 32px 0 0;
  min-width: 125px;

  &.center {
    text-align: center;
    padding: 0 32px;
  }

  span {
    color: rgba(0, 0, 0, .45);
    display: inline-block;
    font-size: .95rem;
    line-height: 42px;
    margin-bottom: 4px;
  }

  p {
    line-height: 42px;
    margin: 0;

    a {
      font-weight: 600;
      font-size: 1rem;
    }
  }
}
</style>
